﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Assistant;
using Assistant.KuaiPan;
using Assistant.WebHelper;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using YoucaiReader.Entities.KuaiPan;

namespace YoucaiReader
{
    public partial class TestPage : PhoneApplicationPage
    {
        public TestPage()
        {
            InitializeComponent();

            //



        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string requestTokenUrl = new KuaiPanHelper().GetRequestToken();
            LogHelper.LogInfo("requestTokenUrl:" + requestTokenUrl, this.GetType());
            //
            string requestToken = string.Empty;
            WebTaskHelper webTask = new WebTaskHelper(new Uri(requestTokenUrl.ToString()));
            IAsyncResult ia = webTask.BeginWebTask(delegate(IAsyncResult callBackResult)
            {
                requestToken = webTask.EndWebTask(callBackResult);
                LogHelper.LogInfo(requestToken);
                RequestToken modelRequestToken = JSONHelper.Deserialize<RequestToken>(requestToken);
                if (modelRequestToken != null)
                {
                    LogHelper.LogInfo(modelRequestToken.OAuthToken + "\t" + modelRequestToken.OAuthTokenSecret);
                    //authorize
                    // string requestAuthorizeUrl = new KuaiPanHelper().GetAuthorize(modelRequestToken.OAuthToken);
                    // LogHelper.LogInfo("requestTokenUrl:" + requestTokenUrl, this.GetType());
                    // webTask = new WebTaskHelper(new Uri(requestAuthorizeUrl.ToString()));
                    // ia = webTask.BeginWebTask(delegate(IAsyncResult callBackResult2)
                    //{
                    //    string authorize = webTask.EndWebTask(callBackResult2);
                    //    //LogHelper.LogInfo(authorize);
                    //    this.Dispatcher.BeginInvoke(() =>
                    //    {
                    //        this.webBrowser.NavigateToString(authorize);
                    //    });
                    //}, webTask);
                    //accessToken
                    //string accessTokenUrl = new KuaiPanHelper().GetAccessToken(modelRequestToken.OAuthToken);
                    ////LogHelper.LogInfo("requestTokenUrl:" + accessTokenUrl, this.GetType());
                    //webTask = new WebTaskHelper(new Uri(accessTokenUrl.ToString()));
                    //ia = webTask.BeginWebTask(delegate(IAsyncResult iarAccessToken)
                    //{
                    //    string accountInfo = webTask.EndWebTask(iarAccessToken);
                    //    LogHelper.LogInfo(accountInfo);
                    //}, webTask);
                    //accountInfo
                    string accoutInfoUrl = new KuaiPanHelper().GetAccountInfo(modelRequestToken.OAuthToken);
                    LogHelper.LogInfo("requestTokenUrl:" + accoutInfoUrl, this.GetType());
                    webTask = new WebTaskHelper(new Uri(accoutInfoUrl.ToString()));
                    ia = webTask.BeginWebTask(delegate(IAsyncResult iarAccountInfo)
                   {
                       string accountInfo = webTask.EndWebTask(iarAccountInfo);
                       LogHelper.LogInfo(accountInfo);
                   }, webTask);

                }
            }, webTask);

        }


    }
}